package com.fire.arthas.demo.web;

import com.fire.arthas.demo.bean.ParamInfo;
import com.fire.arthas.demo.service.TestService;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  Mr.Fire
 * @date    2022/7/19
 * @desc    
 */
@RestController
@RequestMapping("/test")
public class ArthasTestController {
    
    private final static String lockA = "a";
    private final static String lockB = "b";
    
    @Autowired
    TestService testService;

    private final Logger logger = LoggerFactory.getLogger(ArthasTestController.class);


    /** 
     * java -jar  -Xms=20m -Xmx=20m
     */
    @GetMapping("/jvm")
    public String testJvm() throws InterruptedException {
        byte[] bytes = new byte[5*1024*1024];
        Thread.sleep(1000L);
        byte[] bytes2 = new byte[5*1024*1024];
        Thread.sleep(1000L);
        byte[] bytes3 = new byte[5*1024*1024];
        Thread.sleep(1000L);
        byte[] bytes4 = new byte[5*1024*1024];
        return "success";
    }

    @GetMapping("/cpu")
    public String testCpu(){
        for(;;){
           int i =1525522/2556;
        }
    }
    
    @GetMapping("/thread")
    public String testThread(){
        new Thread(()-> {
            synchronized (lockA){
                try {
                    System.out.println("线程1获取到锁A");
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1准备获取锁B");
                synchronized (lockB){
                    System.out.println("线程1获取到锁B");
                }
            }
        }).start();


        new Thread(()-> {
            synchronized (lockB){
                try {
                    System.out.println("线程2获取到锁B");
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2准备获取锁A");
                synchronized (lockA){
                    System.out.println("线程1获取到锁A");
                }
            }
        }).start();
        
        return "success";
    }

    @SneakyThrows
    @GetMapping("/watch")
    public List<String> watchMethod(Integer id){
        List<String> res = new ArrayList<String>();
        int index=id;
        while(index<10){
            res.add(index+++"");
        }
        return res;
    }


    @SneakyThrows
    @PostMapping("/watch-post")
    public List<String> watchMethod2(ParamInfo paramInfo){
        List<String> res = new ArrayList<String>();
        int index = paramInfo.getId();
        while(index<10){
            res.add(index+++"");
        }
        return res;
    }
    
    @SneakyThrows
    @GetMapping("/trace")
    public String traceMethod(){
        testService.traceMethod1();
        testService.traceMethod2();
        Thread.sleep(1000L);
        testService.traceMethod3();
        return "success";
    }

    @GetMapping("/monitor")
    public void monitorMethod(){
        int index=0;
        while(index<15){
            try {
                testService.montorMethod(index);
                index++;
            }catch (Exception e){
                index++;
                logger.error("调用失败！");
            }
        }
    }

    @GetMapping("/logger")
    public String loggerMethod(String name){
        logger.debug("进入logger方法！,参数name："+name);
        if (StringUtils.hasLength(name)){
            logger.debug("获取到参数name");
            return name.concat("_sutifix");
        }
        logger.debug("获取到参数为空,返回错误!");
        return "error";
    }
    
}
